{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 列表的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "['a', 'b', 'c']\n",
      "[1, 'a', True]\n"
     ]
    }
   ],
   "source": [
    "# 创建列表\n",
    "list1 = [1,2,3]\n",
    "list2 = ['a','b','c']\n",
    "list3 = [1,'a',True]\n",
    "print(list1)\n",
    "print(list2)\n",
    "print(list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "[2, 3]\n",
      "[100, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "# 访问列表元素\n",
    "print(list1)\n",
    "print(list1[0]) \n",
    "print(list1[-1])\n",
    "print(list1[1:3])\n",
    "\n",
    "list1[0] = 100\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 遍历列表\n",
    "for i in range(len(list1)):\n",
    "    print(list1[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表的切片\n",
    "# 切片获取列表索引从N到M（不包含M），以K为步长的元素组成新的列表,切片使用方式：\n",
    "# <列表名>[N: M: K]\n",
    "# N:开始索引，正索引默认为0，负索引默认为-len(列表名）\n",
    "# M:结束索引，不包含\n",
    "# K:步长，默认为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3]\n",
      "[1, 2, 3]\n",
      "[2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
      "[2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "list4 = [1,2,3,4,5,6,7,8,9,10]\n",
    "print(list4[1:3]) \n",
    "print(list4[:3]) \n",
    "print(list4[1:]) \n",
    "print(list4[1:6:2]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 列表的常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "10\n",
      "1\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
      "55\n",
      "['a', 'b', 'c', '1', '2', '3']\n"
     ]
    }
   ],
   "source": [
    "print(len(list4))\n",
    "print(max(list4))\n",
    "print(min(list4))\n",
    "print(sorted(list4))\n",
    "print(sum(list4))\n",
    "print(list(\"abc123\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 列表的常用操作方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "list4.append(100)\n",
    "list4.append(200)\n",
    "print(list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10000, 1000, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200]\n"
     ]
    }
   ],
   "source": [
    "list4.insert(0,1000)\n",
    "list4.insert(0,10000)\n",
    "print(list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# list4.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list4.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list4.remove(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[200, 100, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1000, 10000]\n"
     ]
    }
   ],
   "source": [
    "list4.reverse()\n",
    "print(list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[200, 100, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1000, 10000]\n"
     ]
    }
   ],
   "source": [
    "list5 = list4.copy()\n",
    "print(list5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(list5.index(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200, 1000, 10000]\n"
     ]
    }
   ],
   "source": [
    "list5.sort()\n",
    "print(list5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "print(list5.count(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
